package com.deyuanyun.pic.docmger.service.impl;

import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.docmger.controller.dto.DocStandardDTO;
import com.deyuanyun.pic.docmger.controller.dto.DocStandardQueryVO;
import com.deyuanyun.pic.docmger.domain.DocStandard;
import com.deyuanyun.pic.docmger.domain.DocStandardExample;
import com.deyuanyun.pic.docmger.mapper.DocStandardExpandMapper;
import com.deyuanyun.pic.docmger.mapper.DocStandardMapper;
import com.deyuanyun.pic.docmger.service.DocStandardService;
import com.deyuanyun.pic.settings.domain.base.Attachment;
import com.deyuanyun.pic.settings.domain.util.DomainInitUtil;
import com.deyuanyun.pic.settings.mapper.base.AttachmentMapper;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 文档中心---技术标准service实现
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-11-14
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
@Service
public class DocStandardServiceImpl implements DocStandardService {

    @Autowired
    private DocStandardMapper docStandardMapper;

    @Autowired
    private AttachmentMapper attachmentMapper;

    @Autowired
    private DocStandardExpandMapper docStandardExpandMapper;

    @Override
    public DocStandardDTO selectById(String id) {
        List<DocStandardDTO> docStandardDTOs = docStandardExpandMapper.queryStandard(new DocStandardQueryVO().setId(id));
        if(docStandardDTOs == null || docStandardDTOs.isEmpty()) {
            return null;
        }
        return docStandardDTOs.get(0);
    }

    @Override
    public List<DocStandardDTO> queryStandard(DocStandardQueryVO queryVO) {
       return docStandardExpandMapper.queryStandard(queryVO);
    }

    @Override
    public void deleteStandardInfo(String id) {
        DocStandardExample standardExample = new DocStandardExample();
        standardExample.createCriteria().andDeletedEqualTo((short) 0).andIdEqualTo(id);
        DocStandard docStandard = new DocStandard();
        docStandard.setDeleted((short) 1);
        DomainInitUtil.initUpdateDomain(docStandard, "删除技术标准");
        docStandardMapper.updateByExampleSelective(docStandard, standardExample);
        attachmentMapper.updateByBusinessId(id);
    }

    @Override
    public void updateStandardInfo(DocStandard docStandard, String attachmentId) {
        DocStandardQueryVO queryVO = new DocStandardQueryVO().setCode(docStandard.getCode()).setExcludedId(Lists.newArrayList(docStandard.getId()));
        List<DocStandardDTO> dtos = docStandardExpandMapper.queryStandard(queryVO);
        Asserts.assertTrue(dtos == null || dtos.isEmpty(), "标准编号已存在，请重新输入");
        queryVO.setCode("").setName(docStandard.getName());
        dtos = docStandardExpandMapper.queryStandard(queryVO);
        Asserts.assertTrue(dtos == null || dtos.isEmpty(), "标准名称已存在，请重新输入");

        DocStandardExample standardExample = new DocStandardExample();
        standardExample.createCriteria().andDeletedEqualTo((short) 0).andIdEqualTo(docStandard.getId());
        DomainInitUtil.initUpdateDomain(docStandard, "更新技术标准");
        docStandardMapper.updateByExampleSelective(docStandard, standardExample);
        if(StringUtils.isNotEmpty(attachmentId)) {
            String id = docStandard.getId();
            Attachment attachment = attachmentMapper.selectByPrimaryKey(attachmentId);
            if(attachment != null) {
                attachmentMapper.updateByBusinessId(id);
                attachment.setBusinessId(id);
                attachmentMapper.updateByPrimaryKeySelective(attachment);
            }
        }
    }

    @Override
    public void addStandardInfo(DocStandard docStandard, String attachmentId) {
        DocStandardQueryVO queryVO = new DocStandardQueryVO().setCode(docStandard.getCode());
        List<DocStandardDTO> dtos = docStandardExpandMapper.queryStandard(queryVO);
        Asserts.assertTrue(dtos == null || dtos.isEmpty(), "标准编号已存在，请重新输入");
        queryVO.setCode("").setName(docStandard.getName());
        dtos = docStandardExpandMapper.queryStandard(queryVO);
        Asserts.assertTrue(dtos == null || dtos.isEmpty(), "标准名称已存在，请重新输入");

        DomainInitUtil.initCreateDomain(docStandard);
        docStandardMapper.insertSelective(docStandard);
        if(StringUtils.isNotEmpty(attachmentId)) {
            String id = docStandard.getId();
            Attachment attachment = attachmentMapper.selectByPrimaryKey(attachmentId);
            if(attachment != null) {
                String businessId = attachment.getBusinessId();
                if (StringUtils.isNotEmpty(businessId)) {
                    DomainInitUtil.initCreateDomain(attachment);
                    attachment.setBusinessId(id);
                    attachmentMapper.insertSelective(attachment);
                } else {
                    DomainInitUtil.initUpdateDomain(attachment, "关联附件");
                    attachment.setBusinessId(id);
                    attachmentMapper.updateByPrimaryKeySelective(attachment);
                }
            }
        }
    }
}
